package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import java.util.ArrayList;
import ControlLayer.*;//The TUILayer needs to know the ControlLayer
import ModelLayer.*; // The TUILayer needs access to the ModelLayer to return certain methods correctly.
 

/**
 * User interface class to the AddressBook
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressBookUI
{   

    private AddressCtr addrCtr;//reference to the ControlLayer
    boolean returnTo;
    
    //constructor which initialises the AddressController for usage.
    public AddressBookUI()
    {
       addrCtr = new AddressCtr();
    }
    
    public void start()
    {
       addressMenu();
    }
    
    public void addressMenu()
    {
       boolean exit = false;
       while(!exit)//that is: while exit is false
       {
          int choice = writeAddressBookMenu();
          if(choice == 1)
          {
              // Read Person
              String name = inputName();
              Person p = addrCtr.readPerson(name);
              System.out.print("\f");
              System.out.println("Name       : " + p.getName());
              System.out.println("Address    : " + p.getAddress());
              System.out.println("PostalCode : " + p.getPostalCode());
              System.out.println("City       : " + p.getCity());
              System.out.println("PhoneNo    : " + p.getPhoneNo());
              returnTo = false;
              while(!returnTo)
              {
                  pressAnyKey();
              }
          }
          else
          {
              if(choice == 2)
              {
                    //create person
                    String name = inputName();
                    String address = inputAddress();
                    String postalCode = inputPostalCode();
                    String city = inputCity();
                    int phoneNo = inputPhoneNo();
                    
                    addrCtr.createPerson(name, address, postalCode, city, phoneNo);
              }  
              else{
                  if(choice == 3)
                  {
                      //delete person
                      String name = inputName();
                      addrCtr.deletePerson(name);
                  }  
                  else{
                      if(choice == 4)
                      {
                          // update person
                          String name = inputName();
                          String address = inputAddress();
                          String postalCode = inputPostalCode();
                          String city = inputCity();
                          int phoneNo = inputPhoneNo();
                      }
                      else
                      {
                          if(choice == 5)
                          {
                              // listAllPersons
                              ArrayList<Person> p = addrCtr.listAllPersons();
                              System.out.println("\f*** People in Address Book ***");
                              for(Person Person : p)
                              {
                                  System.out.print("\n");
                                  System.out.println("Name        : " + Person.getName());
                                  System.out.println("Address     : " + Person.getAddress());
                                  System.out.println("Postal Code : " + Person.getPostalCode());
                                  System.out.println("City        : " + Person.getCity());
                                  System.out.println("Phone No    : " + Person.getPhoneNo());
                              }
                              returnTo = false;
                              while(!returnTo)
                              {
                                  pressAnyKey();
                              }
                          }
                          else{
                              exit = true;
                           }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
            
       }//end while
    }
    public int writeAddressBookMenu()
    {      // makes an object keyboard to read input from the screen
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** AddressBook Menu ***");
            System.out.println(" (1) Show information about person");
            System.out.println(" (2) Add person");
            System.out.println(" (3) Delete person");
            System.out.println(" (4) Update person");
            System.out.println(" (5) List all persons");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            return choice;
        }
           
    public void pressAnyKey()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println("\nPress any key to continue:  ");
         keyboard.nextLine();
         returnTo = true;
    }
    
    public String inputName()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Name:  ");
         String name = keyboard.nextLine();
         return name;
    }
   
    public String inputPostalCode()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Postal Code:  ");
         String code = keyboard.nextLine();
         return code;
    }
    public String inputCity()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write City:  ");
         String city = keyboard.nextLine();
         return city;
    } 
    public int inputPhoneNo()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Phone Number:  ");
         int phone = keyboard.nextInt();
         return phone;
    } 
     
    public String  inputAddress()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Address: ");
        String address = keyboard.nextLine();
        return address;
    }
    
    public void showId(int id)
    {
        System.out.println("ID of the person: " + id);
    }
    public void showName(String name)
    { 
        System.out.println("The name of the persone: " + name);
    }
    
    public void showAddress(String address)
    { 
        System.out.println("The address of the person: " + address);
    }  
}
            
        
